<template>
	<jk-dialog
		v-model="saveDialogState"
		:title="`${!dataId ? '新增' : '编辑'}-用户`"
		width="460px"
		:confirm-btn-loading="confirmBtnLoading"
		:show-content-loading="showContentLoading"
		@on-confirm="onConfirmButton"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<el-form ref="ruleForm" class="dialog-view" :model="ruleForm" :rules="rules" :show-message="false" size="medium" label-width="90px">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="昵称" prop="nickname">
						<el-input v-model="ruleForm.nickname" placeholder="昵称" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="账号" prop="username">
						<el-input v-model="ruleForm.username" placeholder="账号" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item v-if="!dataId" class="margin-bottom-10" label="密码" prop="password">
						<el-input v-model="ruleForm.password" placeholder="密码" type="password" show-password />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="性别">
						<el-radio-group v-model="ruleForm.sex">
							<el-radio border class="margin-right-10" :label="1">男</el-radio>
							<el-radio border :label="0">女</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="备注">
						<el-input v-model="ruleForm.remark" type="textarea" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>
<script>
    import { addUser, getUser, updateUser } from '../../../api/system/user';
    import { validateNull, validateCode } from '../../../utils/jk/validator';

    import calDialogHeight from '../../../mixins/cal-dialog-height';
    export default {
        mixins: [calDialogHeight],
        inheritAttrs: false,
        props: {
            deptId: {
                type: Number,
                default: null
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: Number,
                default: null
            }
        },
        data() {
            return {
                showContentLoading: false,
                saveDialogState: false,
                confirmBtnLoading: false,
                ruleForm: {
                    nickname: '',
                    username: '',
                    password: '',
                    sex: 1,
                    remark: ''
                },
                rules: {
                    nickname: [{ required: true, validator: validateNull, trigger: 'change' }],
                    username: [{ required: true, validator: validateNull, trigger: 'change' }],
                    sex: [{ required: true, validator: validateCode, trigger: 'change' }],
                    password: [{ required: true, validator: validateNull, trigger: 'change' }]
                }
            };
        },
        watch: {
            dialogState(newVal) {
                this.saveDialogState = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        },
        methods: {
            getDetailRequest() {
                this.showContentLoading = true;
                return getUser(this.dataId).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                        this.$set(this.ruleForm, 'userAccount', '');
                        this.$set(this.ruleForm, 'userPwd', null);
                        this.showContentLoading = false;
                    }
                });
            },
            async getDependentData() {
                this.showContentLoading = true;
                if (this.dataId) await this.getDetailRequest();
                this.showContentLoading = false;
            },
            onVisibleChange(e) {
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs['ruleForm'].resetFields();
                    });
                }
                this.$emit('on-visible-change', e);
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) delete params.id;
                return params;
            },
            createAndUpdateRequest() {
                const api = !this.dataId ? addUser : updateUser;
                this.confirmBtnLoading = true;
                api(this.getParamData()).then(res => {
                    this.confirmBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-confirm');
                    }
                });
            },
            onConfirmButton() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            }
        }
    };
</script>
